package cn.njyd.corejava.thread.myday04;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

public class CompletableFutureDemo01 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"正在处理计算结果");
            int n = ThreadLocalRandom.current().nextInt(1000);
            int m = 10/0;
            System.out.println("计算完成!");
            return n;
        },pool).whenComplete((v,e)->{
            if (e == null) {
                System.out.println(v);
            }
        }).exceptionally(e->{
            if (e != null){
                System.out.println(e.getMessage());
            }
            return 0;
        });
        System.out.println(Thread.currentThread().getName()+"专注于做自己的事情");
        pool.shutdown();
    }
}
